<template>
  <div class="main">
    <el-card style="max-width: 50%">
      <template #header>
        <div class="card-header">
          <span>用户修改昵称</span>
        </div>
      </template>
      <el-form label-width="auto" style="max-width: 600px" :model="form">
        <el-form-item label="用户名">
          <!-- value是input输入框中的值属性，前面加冒号证明是动态的 -->
          <el-input :value="useStore.username" disabled />
        </el-form-item>
        <el-form-item label="昵 称" prop="new_nickname">
          <el-input v-model="form.new_nickname" />
        </el-form-item>
        <div class="center">
          <el-button type="primary" @click="submit">确认修改</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
// 引入用户小仓库
import useUserStore from '@/store/modules/user'
// 引入接口
import { reqNickname } from '@/api/user/index'
import { ElNotification } from 'element-plus'

const useStore = useUserStore() // 创建用户实例

const form = reactive({
  new_nickname: '',
})
// 确认修改
const submit = async () => {
  console.log('数据为', useStore.userId, form)
  let res = await reqNickname(useStore.userId, form.new_nickname)
  console.log('结果为:', res)
  if (res.code === 200) {
    ElNotification({
      message: res.msg,
      type: 'success',
    })
    // 点击过后刷新
    location.reload()
  } else {
    ElNotification({
      message: res.error_msg,
      type: 'error',
    })
  }
}
// 重置
const reset = () => {
  form.new_nickname = ''
}
</script>

<style lang="scss" scoped>
.main {
  .card-header {
    text-align: center;
    font-size: 25px;
  }
  .center {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
